﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace Spider
{
    /// <summary>
    /// 公用的数据
    /// </summary>
    public class CompData
    {
        XmlHelper xmlHelper = new XmlHelper(Environment.CurrentDirectory + "\\Template\\template.xml");
        /// <summary>
        /// 绑定Http请求的编码格式
        /// </summary>
        /// <returns></returns>
        public static DataTable GetRequestEnCode()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("text", typeof(string));
            dt.Columns.Add("value", typeof(string));
            DataRow dr = dt.NewRow();
            dr["text"] = "UTF-8";
            dr["value"] = "UTF-8";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["text"] = "GB2312";
            dr["value"] = "GB2312";
            dt.Rows.Add(dr);
            return dt;
        }
        public XmlHelper CompXmlHelper
        {
            get { return xmlHelper = new XmlHelper(Environment.CurrentDirectory + "\\Template\\template.xml"); }
            set { xmlHelper = value; }
        }
        /// <summary>
        /// 绑定模板列表
        /// </summary>
        /// <param name="combox"></param>
        public void BindTemplate(System.Windows.Forms.ComboBox combox)
        {
            #region 绑定模板列表
            DataTable dt = xmlHelper.GetAllData("Template/Website");
            dt.DefaultView.Sort = "modifydate desc";
            combox.DataSource = dt.DefaultView;
            combox.DisplayMember = "name";
            combox.ValueMember = "tempid";
            //combox.SelectedIndex = 1;
            //combox.SelectedIndex = 1;
            #endregion
        }       /// <summary>
        /// 绑定模板列表
        /// </summary>
        /// <param name="combox"></param>
        public void BindTemplate(System.Windows.Forms.ComboBox combox, string str)
        {
            #region 绑定模板列表
            DataTable dt = xmlHelper.GetAllData("Template/Website");
            combox.DataSource = dt;
            combox.DisplayMember = "name";
            combox.ValueMember = "tempid";
            combox.SelectedValue = str;
            //combox.SelectedIndex = 1;
            #endregion
        }
        public Hashtable GetTemplateData(string tempid)
        {
            Hashtable ht = new Hashtable();
            DataTable dt = xmlHelper.GetData("Template/Website[@tempid='" + tempid + "']").Table;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                ht[dt.Columns[i].ColumnName] = dt.Rows[0][i].ToString();
            }
            return ht;
        }
    }
}
